<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>libwebsockets: Sending data</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="libwebsockets.org-logo.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">libwebsockets
   </div>
   <div id="projectbrief">Lightweight C library for HTML5 websockets</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li class="current"><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__sending-data.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="summary">
<a href="#enum-members">Enumerations</a> &#124;
<a href="#func-members">Functions</a>  </div>
  <div class="headertitle">
<div class="title">Sending data</div>  </div>
</div><!--header-->
<div class="contents">
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="enum-members"></a>
Enumerations</h2></td></tr>
<tr class="memitem:ga98b099cf8c1c7e38ad78501f270e193d"><td class="memItemLeft" align="right" valign="top">enum &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__sending-data.html#ga98b099cf8c1c7e38ad78501f270e193d">lws_write_protocol</a> { <br />
&#160;&#160;<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da80e8f169fda236c56bfb795ed62903db">LWS_WRITE_TEXT</a> = 0, 
<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193daf6486c0dba50c44198100717721d9ab2">LWS_WRITE_BINARY</a> = 1, 
<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da10047eb05b5e1c298151dc47a5b44826">LWS_WRITE_CONTINUATION</a> = 2, 
<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dabb6705e1d1327cdda5025be28f07712e">LWS_WRITE_HTTP</a> = 3, 
<br />
&#160;&#160;<b>LWS_WRITE_PING</b> = 5, 
<b>LWS_WRITE_PONG</b> = 6, 
<b>LWS_WRITE_HTTP_FINAL</b> = 7, 
<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dafe5a38e940ce56708ac814627e9c0917">LWS_WRITE_HTTP_HEADERS</a> = 8, 
<br />
&#160;&#160;<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da115440f272a5d55518adfc8099acfee3">LWS_WRITE_NO_FIN</a> = 0x40, 
<a class="el" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da220d8e8652d9b97fb66e476e2a60ffce">LWS_WRITE_CLIENT_IGNORE_XOR_MASK</a> = 0x80
<br />
 }</td></tr>
<tr class="separator:ga98b099cf8c1c7e38ad78501f270e193d"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gafd5fdd285a0e25ba7e3e1051deec1001"><td class="memItemLeft" align="right" valign="top">LWS_VISIBLE LWS_EXTERN int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__sending-data.html#gafd5fdd285a0e25ba7e3e1051deec1001">lws_write</a> (struct lws *wsi, unsigned char *buf, size_t len, enum <a class="el" href="group__sending-data.html#ga98b099cf8c1c7e38ad78501f270e193d">lws_write_protocol</a> protocol)</td></tr>
<tr class="separator:gafd5fdd285a0e25ba7e3e1051deec1001"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<p>APIs related to writing data on a connection </p>
<h2 class="groupheader">Enumeration Type Documentation</h2>
<a class="anchor" id="ga98b099cf8c1c7e38ad78501f270e193d"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">enum <a class="el" href="group__sending-data.html#ga98b099cf8c1c7e38ad78501f270e193d">lws_write_protocol</a></td>
        </tr>
      </table>
</div><div class="memdoc">

<p><code>#include &lt;<a class="el" href="libwebsockets_8h.html">lib/libwebsockets.h</a>&gt;</code></p>
<table class="fieldtable">
<tr><th colspan="2">Enumerator</th></tr><tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193da80e8f169fda236c56bfb795ed62903db"></a>LWS_WRITE_TEXT&#160;</td><td class="fielddoc">
<p>Send a ws TEXT message,the pointer must have LWS_PRE valid memory behind it. The receiver expects only valid utf-8 in the payload </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193daf6486c0dba50c44198100717721d9ab2"></a>LWS_WRITE_BINARY&#160;</td><td class="fielddoc">
<p>Send a ws BINARY message, the pointer must have LWS_PRE valid memory behind it. Any sequence of bytes is valid </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193da10047eb05b5e1c298151dc47a5b44826"></a>LWS_WRITE_CONTINUATION&#160;</td><td class="fielddoc">
<p>Continue a previous ws message, the pointer must have LWS_PRE valid memory behind it </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193dabb6705e1d1327cdda5025be28f07712e"></a>LWS_WRITE_HTTP&#160;</td><td class="fielddoc">
<p>Send HTTP content </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193dafe5a38e940ce56708ac814627e9c0917"></a>LWS_WRITE_HTTP_HEADERS&#160;</td><td class="fielddoc">
<p>Send http headers (http2 encodes this payload and LWS_WRITE_HTTP payload differently, http 1.x links also handle this correctly. so to be compatible with both in the future,header response part should be sent using this regardless of http version expected) </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193da115440f272a5d55518adfc8099acfee3"></a>LWS_WRITE_NO_FIN&#160;</td><td class="fielddoc">
<p>This part of the message is not the end of the message </p>
</td></tr>
<tr><td class="fieldname"><a class="anchor" id="gga98b099cf8c1c7e38ad78501f270e193da220d8e8652d9b97fb66e476e2a60ffce"></a>LWS_WRITE_CLIENT_IGNORE_XOR_MASK&#160;</td><td class="fielddoc">
<p>client packet payload goes out on wire unmunged only useful for security tests since normal servers cannot decode the content if used </p>
</td></tr>
</table>
<div class="fragment"><div class="line"><a name="l03173"></a><span class="lineno"> 3173</span>&#160;                        {</div><div class="line"><a name="l03174"></a><span class="lineno"> 3174</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da80e8f169fda236c56bfb795ed62903db">LWS_WRITE_TEXT</a>                                          = 0,</div><div class="line"><a name="l03178"></a><span class="lineno"> 3178</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193daf6486c0dba50c44198100717721d9ab2">LWS_WRITE_BINARY</a>                                        = 1,</div><div class="line"><a name="l03181"></a><span class="lineno"> 3181</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da10047eb05b5e1c298151dc47a5b44826">LWS_WRITE_CONTINUATION</a>                                  = 2,</div><div class="line"><a name="l03184"></a><span class="lineno"> 3184</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dabb6705e1d1327cdda5025be28f07712e">LWS_WRITE_HTTP</a>                                          = 3,</div><div class="line"><a name="l03187"></a><span class="lineno"> 3187</span>&#160;        <span class="comment">/* LWS_WRITE_CLOSE is handled by lws_close_reason() */</span></div><div class="line"><a name="l03188"></a><span class="lineno"> 3188</span>&#160;        LWS_WRITE_PING                                          = 5,</div><div class="line"><a name="l03189"></a><span class="lineno"> 3189</span>&#160;        LWS_WRITE_PONG                                          = 6,</div><div class="line"><a name="l03190"></a><span class="lineno"> 3190</span>&#160;</div><div class="line"><a name="l03191"></a><span class="lineno"> 3191</span>&#160;        <span class="comment">/* Same as write_http but we know this write ends the transaction */</span></div><div class="line"><a name="l03192"></a><span class="lineno"> 3192</span>&#160;        LWS_WRITE_HTTP_FINAL                                    = 7,</div><div class="line"><a name="l03193"></a><span class="lineno"> 3193</span>&#160;</div><div class="line"><a name="l03194"></a><span class="lineno"> 3194</span>&#160;        <span class="comment">/* HTTP2 */</span></div><div class="line"><a name="l03195"></a><span class="lineno"> 3195</span>&#160;</div><div class="line"><a name="l03196"></a><span class="lineno"> 3196</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dafe5a38e940ce56708ac814627e9c0917">LWS_WRITE_HTTP_HEADERS</a>                                  = 8,</div><div class="line"><a name="l03203"></a><span class="lineno"> 3203</span>&#160;        <span class="comment">/****** add new things just above ---^ ******/</span></div><div class="line"><a name="l03204"></a><span class="lineno"> 3204</span>&#160;</div><div class="line"><a name="l03205"></a><span class="lineno"> 3205</span>&#160;        <span class="comment">/* flags */</span></div><div class="line"><a name="l03206"></a><span class="lineno"> 3206</span>&#160;</div><div class="line"><a name="l03207"></a><span class="lineno"> 3207</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da115440f272a5d55518adfc8099acfee3">LWS_WRITE_NO_FIN</a> = 0x40,</div><div class="line"><a name="l03210"></a><span class="lineno"> 3210</span>&#160;        <a class="code" href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da220d8e8652d9b97fb66e476e2a60ffce">LWS_WRITE_CLIENT_IGNORE_XOR_MASK</a> = 0x80</div><div class="line"><a name="l03214"></a><span class="lineno"> 3214</span>&#160;};</div><div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193dabb6705e1d1327cdda5025be28f07712e"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dabb6705e1d1327cdda5025be28f07712e">LWS_WRITE_HTTP</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3184</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193da115440f272a5d55518adfc8099acfee3"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da115440f272a5d55518adfc8099acfee3">LWS_WRITE_NO_FIN</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3207</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193da80e8f169fda236c56bfb795ed62903db"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da80e8f169fda236c56bfb795ed62903db">LWS_WRITE_TEXT</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3174</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193daf6486c0dba50c44198100717721d9ab2"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193daf6486c0dba50c44198100717721d9ab2">LWS_WRITE_BINARY</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3178</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193da220d8e8652d9b97fb66e476e2a60ffce"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da220d8e8652d9b97fb66e476e2a60ffce">LWS_WRITE_CLIENT_IGNORE_XOR_MASK</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3210</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193da10047eb05b5e1c298151dc47a5b44826"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193da10047eb05b5e1c298151dc47a5b44826">LWS_WRITE_CONTINUATION</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3181</div></div>
<div class="ttc" id="group__sending-data_html_gga98b099cf8c1c7e38ad78501f270e193dafe5a38e940ce56708ac814627e9c0917"><div class="ttname"><a href="group__sending-data.html#gga98b099cf8c1c7e38ad78501f270e193dafe5a38e940ce56708ac814627e9c0917">LWS_WRITE_HTTP_HEADERS</a></div><div class="ttdef"><b>Definition:</b> libwebsockets.h:3196</div></div>
</div><!-- fragment -->
</div>
</div>
<h2 class="groupheader">Function Documentation</h2>
<a class="anchor" id="gafd5fdd285a0e25ba7e3e1051deec1001"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">LWS_VISIBLE LWS_EXTERN int lws_write </td>
          <td>(</td>
          <td class="paramtype">struct lws *&#160;</td>
          <td class="paramname"><em>wsi</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">unsigned char *&#160;</td>
          <td class="paramname"><em>buf</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">size_t&#160;</td>
          <td class="paramname"><em>len</em>, </td>
        </tr>
        <tr>
          <td class="paramkey"></td>
          <td></td>
          <td class="paramtype">enum <a class="el" href="group__sending-data.html#ga98b099cf8c1c7e38ad78501f270e193d">lws_write_protocol</a>&#160;</td>
          <td class="paramname"><em>protocol</em>&#160;</td>
        </tr>
        <tr>
          <td></td>
          <td>)</td>
          <td></td><td></td>
        </tr>
      </table>
</div><div class="memdoc">

<p><code>#include &lt;<a class="el" href="libwebsockets_8h.html">lib/libwebsockets.h</a>&gt;</code></p>
<p><a class="el" href="group__sending-data.html#gafd5fdd285a0e25ba7e3e1051deec1001">lws_write()</a> - Apply protocol then write data to client </p><dl class="params"><dt>Parameters</dt><dd>
  <table class="params">
    <tr><td class="paramname">wsi</td><td>Websocket instance (available from user callback) </td></tr>
    <tr><td class="paramname">buf</td><td>The data to send. For data being sent on a websocket connection (ie, not default http), this buffer MUST have LWS_PRE bytes valid BEFORE the pointer. This is so the protocol header data can be added in-situ. </td></tr>
    <tr><td class="paramname">len</td><td>Count of the data bytes in the payload starting from buf </td></tr>
    <tr><td class="paramname">protocol</td><td>Use LWS_WRITE_HTTP to reply to an http connection, and one of LWS_WRITE_BINARY or LWS_WRITE_TEXT to send appropriate data on a websockets connection. Remember to allow the extra bytes before and after buf if LWS_WRITE_BINARY or LWS_WRITE_TEXT are used.</td></tr>
  </table>
  </dd>
</dl>
<p>This function provides the way to issue data back to the client for both http and websocket protocols.</p>
<p>IMPORTANT NOTICE!</p>
<p>When sending with websocket protocol</p>
<p>LWS_WRITE_TEXT, LWS_WRITE_BINARY, LWS_WRITE_CONTINUATION, LWS_WRITE_PING, LWS_WRITE_PONG</p>
<p>the send buffer has to have LWS_PRE bytes valid BEFORE the buffer pointer you pass to <a class="el" href="group__sending-data.html#gafd5fdd285a0e25ba7e3e1051deec1001">lws_write()</a>.</p>
<p>This allows us to add protocol info before and after the data, and send as one packet on the network without payload copying, for maximum efficiency.</p>
<p>So for example you need this kind of code to use lws_write with a 128-byte payload</p>
<p>char buf[LWS_PRE + 128];</p>
<p>// fill your part of the buffer... for example here it's all zeros memset(&amp;buf[LWS_PRE], 0, 128);</p>
<p>lws_write(wsi, &amp;buf[LWS_PRE], 128, LWS_WRITE_TEXT);</p>
<p>When sending HTTP, with</p>
<p>LWS_WRITE_HTTP, LWS_WRITE_HTTP_HEADERS LWS_WRITE_HTTP_FINAL</p>
<p>there is no protocol data prepended, and don't need to take care about the LWS_PRE bytes valid before the buffer pointer.</p>
<p>LWS_PRE is at least the frame nonce + 2 header + 8 length LWS_SEND_BUFFER_POST_PADDING is deprecated, it's now 0 and can be left off. The example apps no longer use it.</p>
<p>Pad LWS_PRE to the CPU word size, so that word references to the address immediately after the padding won't cause an unaligned access error. Sometimes for performance reasons the recommended padding is even larger than sizeof(void *). </p><pre class="fragment"> In the case of sending using websocket protocol, be sure to allocate
 valid storage before and after buf as explained above.  This scheme
 allows maximum efficiency of sending data and protocol in a single
 packet while not burdening the user code with any protocol knowledge.

 Return may be -1 for a fatal error needing connection close, or the
 number of bytes sent.
</pre><h1>Truncated Writes </h1>
<p>The OS may not accept everything you asked to write on the connection.</p>
<p>Posix defines POLLOUT indication from poll() to show that the connection will accept more write data, but it doesn't specifiy how much. It may just accept one byte of whatever you wanted to send.</p>
<p>LWS will buffer the remainder automatically, and send it out autonomously.</p>
<p>During that time, WRITABLE callbacks will be suppressed.</p>
<p>This is to handle corner cases where unexpectedly the OS refuses what we usually expect it to accept. You should try to send in chunks that are almost always accepted in order to avoid the inefficiency of the buffering. </p>

</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
